package club.dataresources.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/*
 * 业务辅助类
 */
public class YujianHelper {

  //获取系统时间并转化为美国时间
  public static String getTime(){
    Date date = new Date();
    TimeZone timeZoneNY = TimeZone.getTimeZone("America/Los_Angeles");
    SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.US);
    outputFormat.setTimeZone(timeZoneNY);
    String AmericanTime = outputFormat.format(date);
    return AmericanTime;
  }

  //计算两个具体日期时间之间的月份差
  public static int getMonthDifference(String timeSmall,String timeBig) throws ParseException{
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
    //String str1 = "2012-02-09 23:12:23";
    String str1 = timeBig;
    String str2 = timeSmall;
    Calendar bef = Calendar.getInstance();
    Calendar aft = Calendar.getInstance();
    bef.setTime(sdf.parse(str1));
    aft.setTime(sdf.parse(str2));
    int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
    int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
    System.out.println(Math.abs(month + result));
    return Math.abs(month + result);
  }

  //指定某个时间距离现在时间的月份差
  public static int getMonthDifference(String time) throws ParseException{
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
    //String str1 = "2012-02-09 23:12:23";
    String str1 = getTime();
    String str2 = time;
    Calendar bef = Calendar.getInstance();
    Calendar aft = Calendar.getInstance();
    bef.setTime(sdf.parse(str1));
    aft.setTime(sdf.parse(str2));
    int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
    int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
    System.out.println(Math.abs(month + result));
    return Math.abs(month + result);
  }

  //指定两个日期时间的天数差
  public static int getDayDifference(String time1,String time2) throws ParseException{
    Date date1 = getDateFromTime(time1);
    Date date2 = getDateFromTime(time2);

    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);

    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);
    int day1= cal1.get(Calendar.DAY_OF_YEAR);
    int day2 = cal2.get(Calendar.DAY_OF_YEAR);

    int year1 = cal1.get(Calendar.YEAR);
    int year2 = cal2.get(Calendar.YEAR);
    if(year1 != year2){
      //同一年
      int timeDistance = 0 ;
      for(int i = year1 ; i < year2 ; i ++){
        if(i%4==0 && i%100!=0 || i%400==0){
          //闰年
          timeDistance += 366;
        }
        else {
          //不是闰年
          timeDistance += 365;
        }
      }

      return timeDistance + (day2-day1) ;
    }
    else{
      //不同年
      //System.out.println("判断day2 - day1 : " + (day2-day1));
      return day2-day1;
    }
  }

  //指定一个日期和当前日期之间的天数差
  public static int getDayDifference(String time) throws ParseException{
    Date date1 = getDateFromTime(time);
    Date date2 = getDateFromTime(getTime());

    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);

    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);
    int day1= cal1.get(Calendar.DAY_OF_YEAR);
    int day2 = cal2.get(Calendar.DAY_OF_YEAR);

    int year1 = cal1.get(Calendar.YEAR);
    int year2 = cal2.get(Calendar.YEAR);
    if(year1 != year2){
      //同一年
      int timeDistance = 0 ;
      for(int i = year1 ; i < year2 ; i ++){
        if(i%4==0 && i%100!=0 || i%400==0){
          //闰年
          timeDistance += 366;
        }
        else {
          //不是闰年
          timeDistance += 365;
        }
      }

      return timeDistance + (day2-day1) ;
    }
    else{
      //不同年
      //System.out.println("判断day2 - day1 : " + (day2-day1));
      return day2-day1;
    }
  }

  //将具体的时间转化为日期
  public static Date getDateFromTime(String time) throws ParseException{
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Date date = sdf.parse(time);
    return date;
  }

  //将具体的时间转化为年月日的时间
  public static String getDateFromTimeForYearMonthDay(String time) throws Exception{
    String  yearMonthDay = time.substring(0,10);
    return yearMonthDay;
  }


  //写一个根据时间判断该时间处于该月份上中下旬的判断方法，0代表下旬，1代表中旬，2代表上旬
  public static int timePositionOfMonth(String time){
    String dateOfTenPosition = time.substring(8,9);
    if(dateOfTenPosition.equals("0")){
      return 2;
    }else if(dateOfTenPosition.equals("1")){
      return 1;
    }else if(dateOfTenPosition.equals("2")){
      return 0;
    }else if(dateOfTenPosition.equals("3")){
      return 0;
    }else{
      return 200;
    }
  }

  //写一个根据月份差返回距离当前月份的	年月的方法
  public static String getMonthFromDiff(int monthDifference){
    Calendar c = Calendar.getInstance();
    c.add(Calendar.MONTH, -monthDifference);
    SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM");
    String time = format.format(c.getTime());
    return time;
  }

  //根据天数差获取距离当天的日期
  public static String getDayFromDiff(int dayDifference){
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

    Calendar c = Calendar.getInstance();
    c.add(Calendar.DATE, -dayDifference);
    String time = sdf.format(c.getTime());
    return time;
  };


  //把指定的日期格式的字符串转换成Date类型
  public static Date stringToDate(String string) {
    String str = "yyyy-MM-hh";
    SimpleDateFormat format = new SimpleDateFormat(str);
    Date date = new Date();
    try {
      date = format.parse(string);
    } catch (Exception e) {
      e.getStackTrace();
    }
    return date;
  }

  //把指定的date类型日期转换成Calendar类型
  public static Calendar dateToCalendar(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    return calendar;
  }


  //将数组元素倒序的方法
  public static String[] testReverseSelf(String[] arry) throws Exception {
    System.out.println("\t" + Arrays.toString(arry));
    for (int start = 0, end = arry.length - 1; start < end; start++, end--) {
      String temp = arry[end];
      arry[end] = arry[start];
      arry[start] = temp;
    }
    System.out.println("\t" + Arrays.toString(arry));
    return arry;
  }

  public static int[] testReverseSelf(int[] arry) throws Exception {
    System.out.println("\t" + Arrays.toString(arry));
    for (int start = 0, end = arry.length - 1; start < end; start++, end--) {
      int temp = arry[end];
      arry[end] = arry[start];
      arry[start] = temp;
    }
    System.out.println("\t" + Arrays.toString(arry));
    return arry;
  }

  public static String getTimeNowDate(){
    Date d = new Date();
    System.out.println(d);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //yyyy-MM-dd HH:mm:ss
    String dateNowStr = sdf.format(d);
    System.out.println("格式化后的日期：" + dateNowStr);
    return dateNowStr;
  }

  public static void main(String[] args) throws Exception {
    getTimeNowDate();
  }
}
